﻿using System;
using System.Collections;
using System.Collections.Generic;

namespace BuzzX8.Xmpp
{
    public class PropertyDictionary : IDictionary<string, object>
    {
        private IDictionary<string, object> valueContainer;
 
        public PropertyDictionary(IDictionary<string, object> initialValues = null)
        {
            valueContainer = new Dictionary<string, object>();
            if (initialValues != null) Merge(initialValues);
        }

        public T Get<T>(string key)
        {
            if (!valueContainer.ContainsKey(key)) return default(T);
            object value = valueContainer[key];
            if (value.GetType() != typeof(T)) throw new ArgumentException();
            return (T) value;
        }

        public void Merge(IDictionary<string, object> parameters)
        {
            foreach (KeyValuePair<string, object> parameter in parameters)
            {
                this[parameter.Key] = parameter.Value;
            }
        }

        #region IDictionary implementation

        public void Add(string key, object value)
        {
            valueContainer.Add(key, value);
        }

        public bool ContainsKey(string key)
        {
            return valueContainer.ContainsKey(key);
        }

        public ICollection<string> Keys
        {
            get { return valueContainer.Keys; }
        }

        public bool Remove(string key)
        {
            return valueContainer.Remove(key);
        }

        public bool TryGetValue(string key, out object value)
        {
            return valueContainer.TryGetValue(key, out value);
        }

        public ICollection<object> Values
        {
            get { return valueContainer.Values; }
        }

        public object this[string key]
        {
            get { return valueContainer[key]; }
            set { valueContainer[key] = value; }
        }

        public void Add(KeyValuePair<string, object> item)
        {
            valueContainer.Add(item);
        }

        public void Clear()
        {
            valueContainer.Clear();
        }

        public bool Contains(KeyValuePair<string, object> item)
        {
            throw new System.NotImplementedException();
        }

        public void CopyTo(KeyValuePair<string, object>[] array, int arrayIndex)
        {
            throw new System.NotImplementedException();
        }

        public int Count
        {
            get { return valueContainer.Count; }
        }

        public bool IsReadOnly
        {
            get { throw new System.NotImplementedException(); }
        }

        public bool Remove(KeyValuePair<string, object> item)
        {
            throw new System.NotImplementedException();
        }

        public IEnumerator<KeyValuePair<string, object>> GetEnumerator()
        {
            return valueContainer.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return valueContainer.GetEnumerator();
        }

        #endregion
    }
}
